using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BardsTaleViewer {
  public class LoaderVersion {
    public int _bardVersion;
    public SystemEnum _system;
    public byte [] _dump;
    public int _nbMax;

    public IPicture _picture;
    public int _pictureIndex;
    public string _filename;

    public LoaderVersion (int bardVersion, SystemEnum system) {
      _bardVersion = bardVersion;
      _system = system;
      _nbMax = 0;

      LoadPicture (-1);

      if (bardVersion == 1) {
        if (system == SystemEnum.Amiga)
          _dump = Properties.Resources.pics1.Extract (0);
        else if (system == SystemEnum.Atari)
          _dump = Properties.Resources.BPICS.Extract (0);
        else
          throw new Exception ();
      }
      else if (bardVersion == 2) {
        if (system == SystemEnum.Amiga)
          _dump = Properties.Resources.pics2.Extract (0);
        else
          throw new Exception ();
      }
      else if (bardVersion == 3) {
        if (system == SystemEnum.Amiga) {
          byte [] dumpAmiga = Properties.Resources.pics3.Extract (0);
          byte [] temp = new byte [dumpAmiga.Length + 1];
          dumpAmiga.CopyTo (0, temp, 0, dumpAmiga.Length);
          _dump = temp;
        }
        else
          throw new Exception ();
      }
      else if (bardVersion == 4) {
        if (system == SystemEnum.Atari)
          throw new Exception ();
      }

      if (system == SystemEnum.Amiga) {
        if ((bardVersion == 1) ||
            (bardVersion == 2))
          _nbMax = (int) _dump.ReadLong (0) / 4;
        else if (bardVersion == 3)
          _nbMax = (int) _dump.ReadLong (0) / 4 - 1;
        else if (bardVersion == 4)
          _nbMax = Picture_Amiga._filenamesBard4.Length;
      }
      else if (system == SystemEnum.Atari) {
        if (bardVersion == 1)
          _nbMax = (int) _dump.ReadLong (0) / 4;
        else
          throw new Exception ();
      }
      else if (system == SystemEnum.PC) {
        if (bardVersion == 4)
          _nbMax = Picture_Amiga._filenamesBard4.Length;
        else
          throw new Exception ();
      }
    }

    public void LoadPicture (int pictureIndex) {
      if ((pictureIndex >= _nbMax) ||
          (pictureIndex < 0)) {
        _picture = null;
        _pictureIndex = -1;
        _filename = null;
        return;
      }

      LoadPicture (-1);
      _pictureIndex = pictureIndex;

      if (_bardVersion == 1) {
        if (_system == SystemEnum.Amiga)
          _picture = Picture_Amiga.Load (null, _dump, pictureIndex, _bardVersion);
        else if (_system == SystemEnum.Atari)
          _picture = Picture_Atari.Load (_dump, pictureIndex);
        else
          throw new Exception ();
      }
      else if (_bardVersion == 2) {
        if (_system == SystemEnum.Amiga) {
          if ((pictureIndex != 25) &&
              (pictureIndex != 30) &&
              (pictureIndex != 52))
            _picture = Picture_Amiga.Load (null, _dump, pictureIndex, _bardVersion);
        }
        else
          throw new Exception ();
      }
      else if (_bardVersion == 3) {
        if (_system == SystemEnum.Amiga)
          _picture = Picture_Amiga.Load (null, _dump, pictureIndex, _bardVersion);
        else
          throw new Exception ();
      }
      else if (_bardVersion == 4) {
        if (_system == SystemEnum.Amiga) {
          _filename = Picture_Amiga._filenamesBard4 [pictureIndex];
          _picture = Picture_Amiga.Load (_filename, null, pictureIndex, _bardVersion);
        }
        else if (_system == SystemEnum.PC) {
          _filename = Picture_Amiga._filenamesBard4 [pictureIndex];
          _picture = Picture_PC.Load (_filename, pictureIndex);
        }
        else
          throw new Exception ();
      }
    }
  }
}
